﻿Public Class ViewModelTableViewerFilter

    Private _viewModelTableViewer As ViewModelTableViewer
    Private _datagrid As AdvancedDatagrid
    Private _columnname As String
    Private _values As New List(Of Object)
    Private _allvalues As New List(Of Object)
    Private _selecteds As New List(Of Object)

    Sub New(viewModelTableViewer As ViewModelTableViewer, commandname As String)
        ' TODO: Complete member initialization 
        _viewModelTableViewer = viewModelTableViewer
        _columnname = commandname

        _values = (From r In _viewModelTableViewer.DataView.ToTable(True, commandname).AsEnumerable()
                  Order By r(0) Ascending
                  Select r(0)).ToList()


        _allvalues = (From r In _viewModelTableViewer.DataSource.DefaultView.ToTable(True, commandname).AsEnumerable()
                      Order By r(0) Ascending
                     Select r(0)).ToList()
    End Sub

    Sub New(datagrid As AdvancedDatagrid, columnname As String)
        _columnname = columnname
        _datagrid = datagrid

        Dim dataview = TryCast(_datagrid.ItemsSource, DataView)

        If dataview Is Nothing Then Return

        _values = (From r In dataview.ToTable(True, columnname).AsEnumerable()
                   Order By If(r(0).GetType() Is GetType(DBNull), "(Vide)", r(0)) Ascending
                   Select If(r(0).GetType() Is GetType(DBNull), "(Vide)", r(0))).ToList()

        '_allvalues = (From r In dataview.Table.DefaultView.ToTable(True, columnname).AsEnumerable()
        '               Order By If(r(0).GetType() Is GetType(DBNull), "(Vide)", r(0)) Ascending
        '               Select If(r(0).GetType() Is GetType(DBNull), "(Vide)", r(0))).ToList()
    End Sub

    Public ReadOnly Property ColumnName As String
        Get
            Return _columnname
        End Get
    End Property

    Public ReadOnly Property Values As List(Of Object)
        Get
            Return _values
        End Get
    End Property
    Public ReadOnly Property AllValues As List(Of Object)
        Get
            Return _allvalues
        End Get
    End Property

    Public Property Selecteds As List(Of Object)
        Get
            Return _selecteds
        End Get
        Set(value As List(Of Object))
            _selecteds = value

            If _viewModelTableViewer IsNot Nothing Then
                Dim filter = _viewModelTableViewer.UpdateFilter()

                MsgBox(filter)

                _viewModelTableViewer.DataView.RowFilter = filter

            Else
                Dim filter = _datagrid.UpdateFilter()

                MsgBox(filter)

                If String.IsNullOrEmpty(_datagrid.DefaultFilter) Then
                    CType(_datagrid.ItemsSource, DataView).RowFilter = filter
                Else
                    CType(_datagrid.ItemsSource, DataView).RowFilter = "(" & _datagrid.DefaultFilter & ") AND (" & filter & ")"

                End If
            End If

        End Set
    End Property


End Class